package uiLayer;

import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;

import controllerLayer.EmployeeCtr;

public class EmployeeUI {
	private EmployeeCtr ectr;
	private static EmployeeUI instance = null;

	public EmployeeUI() {
		ectr = new EmployeeCtr();
	}

	public static EmployeeUI getInstance()       
	{
		if(instance == null)
		{
			instance = new EmployeeUI();
		}
		return instance;
	}
	
	public void employeeMenuStart() {
		boolean exit = false;
		while(!exit){
		int choice = printEmployeeMenu();
		switch(choice)
		{
		 case 1: hireEmployee();
         break;
		 case 2: findByID();
         break;
		 case 3: updateEmployee();
         break;
		 case 4: deleteEmployee();
		 break;
		 case 5: sortByMoney();
         break;
		 case 6: goBack();
         break;
		}
		
		}
		
	}

	private void goBack() {
		OfficeUI.getInstance().run();
		
		
	}

	private void sortByMoney() {
		System.out.println(ectr.sortEmployees());
		
	}

	private void deleteEmployee() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Delete Employee***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        
        ectr.deleteEmployee(id);
		
	}

	private void updateEmployee() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Update Employee***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        System.out.println("Phone Number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Address: ");
        String address = keyboard.nextLine();
        System.out.println("Salary: ");
        double salary = InputParser.getInstance().parseDouble(keyboard.nextLine());
       try{
        ectr.updateEmployee(address,phoneNumber,salary,id);
       }
        catch(NullPointerException npe){
			System.out.println("Employee doesn't exist");
			
		}
	}

	private void findByID() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Find Employee***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        
        System.out.println(ectr.findEmployee(id));
        
		
	}

	private void hireEmployee() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Register Employee***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        System.out.println("Name: ");
        String name = keyboard.nextLine();
        System.out.println("Phone Number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Address: ");
        String address = keyboard.nextLine();
        System.out.println("Salary: ");
        double salary = InputParser.getInstance().parseDouble(keyboard.nextLine());;
        double moneyEarned = 0;
		ectr.createEmployee(id, name, address, phoneNumber, salary, moneyEarned);
		
	}

	private int printEmployeeMenu() {
		Scanner keyboard = new Scanner(System.in);
        System.out.print('\f');
        System.out.println("*** Employee Menu ***");
        System.out.println("1. Hire Employee");
        System.out.println("2. Find Employee");
        System.out.println("3. Update Employee");
        System.out.println("4. Delete Employee");
        System.out.println("5. Sort By Money Earned");
        System.out.println("6. Quit");
        System.out.println("Your choice #: ");
        
        int choice = InputParser.getInstance().parseInt(keyboard.nextLine());
        return choice;
		
	}

}
